package cn.ekuma.epos.datalogic.define.dao.erp;

import java.util.HashMap;
import java.util.List;

import com.openbravo.bean.erp.OrderRuleConfig;
import com.openbravo.bean.erp.OrderType;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.DataParams;
import com.openbravo.data.loader.DataRead;
import com.openbravo.data.loader.DataWrite;
import com.openbravo.data.loader.Datas;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.ImageUtils;
import com.openbravo.data.loader.SerializerReadBasic;
import com.openbravo.data.loader.SerializerReadString;
import com.openbravo.data.loader.SerializerWriteParams;
import com.openbravo.data.loader.SerializerWriteString;
import com.openbravo.data.loader.StaticSentence;
import com.openbravo.data.loader.TableDefinition;
import com.openbravo.data.model.Field;
import com.openbravo.format.Formats;

import cn.ekuma.data.dao.ModifiedLogDAO;
import cn.ekuma.epos.db.table.erp.I_OrderRuleConfig;
import cn.ekuma.epos.db.table.erp.I_OrderState;
import cn.ekuma.util.JsonUtil;


public class OrderRuleConfigDAO extends ModifiedLogDAO<OrderRuleConfig> {

	public OrderRuleConfigDAO(I_Session s) {
		super(s);
	}

	@Override
	public TableDefinition getTable() {
		return new TableDefinition(s,
                "ORDER_RULE_CONFIG",
                new Field[]{
                     new Field(I_OrderRuleConfig.ID,Datas.STRING,Formats.STRING),
                     new Field(I_OrderRuleConfig.ORDERTYPEID,Datas.STRING,Formats.STRING,OrderType.class),
                     new Field(I_OrderRuleConfig.RULENAME,Datas.STRING,Formats.STRING),
                     new Field(I_OrderRuleConfig.PROP,Datas.STRING,Formats.STRING),
                     new Field(I_OrderState.LASTMODIFIED,Datas.TIMESTAMP,Formats.TIMESTAMP)
                    },
                new int[]{0});
	}

	@Override
	public void writeInsertValues(DataWrite dp, OrderRuleConfig obj)
			throws BasicException {
		dp.setString(1,obj.getKey());
		dp.setString(2,obj.getOrderTypeId());
		dp.setString(3,obj.getRuleName());
		dp.setString(4,JsonUtil.toString(obj.getProp()));
		dp.setTimestamp(5,obj.getLastModified());
		
	}

	@Override
	public Class getSuportClass() {
		return OrderRuleConfig.class;
	}

	@Override
	public OrderRuleConfig readValues(DataRead dr, OrderRuleConfig obj)
			throws BasicException {
		if(obj==null)
			obj=new OrderRuleConfig();
		obj.setKey(dr.getString(1));
		obj.setOrderTypeId(dr.getString(2));
		obj.setRuleName(dr.getString(3));
		obj.setProp(JsonUtil.getHashMap(dr.getString(4)));
		obj.setLastModified(dr.getTimestamp(5));
		return obj;
	}

	public HashMap findOrderRulePropByNameAndOrderType(final String orderTypeId,
			final String ruleName) throws BasicException {
		String ret= (String) new StaticSentence(s
                , "SELECT PROP FROM ORDER_RULE_CONFIG WHERE ORDERTYPEID=? AND RULENAME=?"
               ,SerializerWriteParams.INSTANCE, new SerializerReadBasic(new Datas[] {Datas.SERIALIZABLE}) ).find(new DataParams() {
                    public void writeValues() throws BasicException {
                    	setString(1, orderTypeId);
                    	setString(2,ruleName);
                    }});
		return JsonUtil.getHashMap(ret);
	}

	public List<String> findOrderSuperedRuleByOrderType(String orderTypeId) throws BasicException {
		return new StaticSentence(s, 
				"SELECT RULENAME FROM ORDER_RULE_CONFIG WHERE ORDERTYPEID=?  "
				,SerializerWriteString.INSTANCE,SerializerReadString.INSTANCE ).list(orderTypeId);
	}

}
